/*
 * Copyright [duowan.com]
 * Web Site: http://www.duowan.com
 * Since 2005 - 2014
 */

package com.duowan.realtime.dao.impl;

import static com.duowan.common.util.ObjectUtils.isNotEmpty;

import java.util.List;

import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import com.duowan.common.util.ObjectUtils;
import com.duowan.common.util.page.Page;
import com.duowan.realtime.dao.BaseSpringJdbcDao;
import com.duowan.realtime.dao.OverviewBloomfilterDao;
import com.duowan.realtime.model.OverviewBloomfilter;
import com.duowan.realtime.query.OverviewBloomfilterQuery;

/**
 * tableName: overview_bloomfilter
 * [OverviewBloomfilter] 的Dao操作 
 *  
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
*/
public class OverviewBloomfilterDaoImpl extends BaseSpringJdbcDao implements OverviewBloomfilterDao{
	
	private RowMapper<OverviewBloomfilter> entityRowMapper = new BeanPropertyRowMapper<OverviewBloomfilter>(getEntityClass());
	
	static final private String COLUMNS = "bloomfilter_group,bloomfilter_name,partition,server_ip,bitset_size,create_time";
	static final private String SELECT_FROM = "select " + COLUMNS + " from overview_bloomfilter";
	
	@Override
	public Class<OverviewBloomfilter> getEntityClass() {
		return OverviewBloomfilter.class;
	}
	
	@Override
	public String getIdentifierPropertyName() {
		return "bloomfilterGroup";
	}
	
	public RowMapper<OverviewBloomfilter> getEntityRowMapper() {
		return entityRowMapper;
	}
	
	public void insert(OverviewBloomfilter entity) {
		String sql = "insert into overview_bloomfilter " 
			 + " (bloomfilter_group,bloomfilter_name,partition,server_ip,bitset_size) " 
			 + " values "
			 + " (:bloomfilterGroup,:bloomfilterName,:partition,:serverIp,:bitsetSize)";
		//insertWithGeneratedKey(entity,sql); //for sqlserver:identity and mysql:auto_increment
		KeyHolder keyHolder = new GeneratedKeyHolder();
		int affectedRows = getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(entity) , keyHolder);
		//其它主键生成策略
		//insertWithOracleSequence(entity,"sequenceName",sql); //oracle sequence: 
		//insertWithDB2Sequence(entity,"sequenceName",sql); //db2 sequence:
		//insertWithUUID(entity,sql); //uuid
		//insertWithAssigned(entity,sql); //手工分配
	}
	
	public int update(OverviewBloomfilter entity) {
		if (ObjectUtils.isEmpty(getById(entity.getBloomfilterGroup(), entity.getBloomfilterName()))) {
			insert(entity);
			return 0;
		}
		String sql = "update overview_bloomfilter set "
					+ " server_ip=:serverIp,bitset_size=:bitsetSize "
					+ " where  bloomfilter_group = :bloomfilterGroup and bloomfilter_name = :bloomfilterName and partition=:partition";
		return getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(entity));
	}
	
	public int deleteById(String bloomfilterGroup, String bloomfilterName, String partition) {
		String sql = "delete from overview_bloomfilter where  bloomfilter_group = ? and bloomfilter_name = ? and partition=?";
		return  getSimpleJdbcTemplate().update(sql,  bloomfilterGroup,bloomfilterName, partition);
	}

	public OverviewBloomfilter getById(String bloomfilterGroup, String bloomfilterName) {
		String sql = SELECT_FROM + " where  bloomfilter_group = ? and bloomfilter_name = ? ";
		return (OverviewBloomfilter)DataAccessUtils.singleResult(getSimpleJdbcTemplate().query(sql, getEntityRowMapper(),bloomfilterGroup,bloomfilterName));
	}
	

	public Page<OverviewBloomfilter> findPage(OverviewBloomfilterQuery query) {
		
		StringBuilder sql = new StringBuilder("select "+ COLUMNS + " from overview_bloomfilter where 1=1 ");
		if(isNotEmpty(query.getBloomfilterGroup())) {
            sql.append(" and bloomfilter_group = :bloomfilterGroup ");
        }
		if(isNotEmpty(query.getBloomfilterName())) {
            sql.append(" and bloomfilter_name = :bloomfilterName ");
        }
		if(isNotEmpty(query.getServerIp())) {
            sql.append(" and server_ip = :serverIp ");
        }
		if(isNotEmpty(query.getBitsetSize())) {
            sql.append(" and bitset_size = :bitsetSize ");
        }
		
        //sql.append(" order by :sortColumns ");
		
		return pageQuery(sql.toString(),query,getEntityRowMapper());				
	}

	@Override
	public List<OverviewBloomfilter> getByBloomFilterGroup(String bloomfilterGroup) {
		String sql = SELECT_FROM + " where  bloomfilter_group = ? ";
		return getSimpleJdbcTemplate().query(sql, getEntityRowMapper(), bloomfilterGroup);
	}

	@Override
	public List<OverviewBloomfilter> getAll() {
		String sql = " select l.bloomfilter_group,l.partition,l.bloomfilter_name,l.server_ip,l.bitset_size,l.create_time,r.element_number,r.positive_rate,r.timeout " 
				+" FROM(" + SELECT_FROM + ") l "
				+ " inner join "
				+ " (select bloomfilter_group,element_number,positive_rate,timeout from bloomfilter_group) r "
				+ " ON (l.bloomfilter_group=r.bloomfilter_group)";
		return getJdbcTemplate().query(sql, getEntityRowMapper());
	}

	@Override
	public List<OverviewBloomfilter> getByServerIp(String serverIp) {
		String sql = SELECT_FROM + " where  server_ip = ? ";
		return getSimpleJdbcTemplate().query(sql, getEntityRowMapper(), serverIp);
	}

}
